package com.atguigu.system.controller;


import com.atguigu.common.result.Result;
import com.atguigu.model.vo.menu.SysMenuQueryRespVo;
import com.atguigu.model.vo.menu.SysMenuQueryTreeRespVo;
import com.atguigu.model.vo.menu.SysMenuSaveReqVo;
import com.atguigu.model.vo.menu.SysMenuUpdateReqVo;
import com.atguigu.system.service.SysMenuServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author flyduck
 * @since 2023-10-25
 */
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuServiceImpl sysMenuServiceImpl;

    @ApiOperation("菜单列表")
    @GetMapping("/findNodes")
    public Result<List<SysMenuQueryTreeRespVo>> findNodes(){
        List<SysMenuQueryTreeRespVo> list = sysMenuServiceImpl.findNodes();
        return Result.ok(list);
    }

    @ApiOperation("添加菜单")
    @PostMapping("/save")
    public Result<Void> save(@RequestBody SysMenuSaveReqVo reqVo){
        boolean isSuccess = sysMenuServiceImpl.save(reqVo);
        if (isSuccess) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id查询菜单")
    @GetMapping("/findNode/{id}")
    public Result<SysMenuQueryRespVo> findNode(@PathVariable("id")Long id){
        SysMenuQueryRespVo respVo = sysMenuServiceImpl.getById(id);
        return Result.ok(respVo);
    }

    @ApiOperation("修改菜单")
    @PostMapping("/update")
    public Result<Void> update(@RequestBody SysMenuUpdateReqVo reqVo){
        boolean isSuccess = sysMenuServiceImpl.updateById(reqVo);
        if (isSuccess) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id")Long id){
        boolean isSuccess = sysMenuServiceImpl.removeById(id);
        if (isSuccess) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
}

